package com.yegs.jltqc.serviceImpl;

import com.yegs.jltqc.TO.AgePageQuery;
import com.yegs.jltqc.TO.NamePageQuery;
import com.yegs.jltqc.VO.RpAge;
import com.yegs.jltqc.VO.RpStock;
import com.yegs.jltqc.config.ConcurentCache;
import com.yegs.jltqc.dao.RpAgeRepository;
import com.yegs.jltqc.dao.RpStockRepository;
import com.yegs.jltqc.service.RpAgeService;
import com.yegs.jltqc.service.RpStockService;
import com.yegs.jltqc.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class RpAgeServiceImpl implements RpAgeService {

    @Autowired
    private RpAgeRepository rpAgeRepository;

    @Override
    public RpAge save(RpAge rpAge) {
        if(rpAge.getId() == null){
            rpAge.setCreateBy(ConcurentCache.getUserName());
            rpAge.setCreateTime(new Date());
        }else {
            rpAge.setUpdateBy(ConcurentCache.getUserName());
            rpAge.setUpdateTime(new Date());
        }
        return rpAgeRepository.save(rpAge);
    }

    @Override
    public List<RpAge> list() {
        return rpAgeRepository.findAll();
    }

    @Override
    public RpAge get(Long id) {
        return rpAgeRepository.getOne(id);
    }

    @Override
    public void del(Long id) {
        rpAgeRepository.deleteById(id);
    }

    @Override
    public Page<RpAge> query(AgePageQuery query) {
        PageRequest request = PageRequest.of(query.getPageIndex() - 1, query.getPageSize());
        Specification<RpAge> specification = new Specification<RpAge>() {
            @Override
            public Predicate toPredicate(Root<RpAge> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = new ArrayList<Predicate>();
                if(query.getChanpingxinghao() != null && query.getChanpingxinghao().trim().length() > 0){
                    predicates.add(criteriaBuilder.like(root.get("chanpingxinghao"),"%" + query.getChanpingxinghao() + "%"));
                }
                if(query.getShengchandanhao() != null && query.getShengchandanhao().trim().length() > 0){
                    predicates.add(criteriaBuilder.like(root.get("shengchandanhao"),"%" +query.getShengchandanhao() + "%"));
                }
                if(query.getSddate() != null){
                    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get("createTime"),query.getSddate()));
                }
                if(query.getEddate() != null){
                    predicates.add(criteriaBuilder.lessThan(root.get("createTime"), DateUtils.addDay(query.getEddate())));
                }
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return rpAgeRepository.findAll(specification,request);
    }

    @Override
    public Long getNext(Long id) {
        if(id == null){
            RpAge project = rpAgeRepository.findFirstByOrderByIdDesc();
            if(project != null) {
                return project.getId();
            }
        }
        RpAge project = rpAgeRepository.findFirstByIdIsAfter(id);
        if(project == null){ project = rpAgeRepository.findFirstByOrderByIdDesc(); }
        if(project != null){
            return project.getId();
        }
        return null;
    }

    @Override
    public Long getLast(Long id) {
        if(id == null){
            RpAge project = rpAgeRepository.findFirstByOrderByIdDesc();
            if(project != null) {
                return project.getId();
            }
        }
        else{
            RpAge project = rpAgeRepository.findFirstByIdBeforeOrderByIdDesc(id);
            if(project == null){ project = rpAgeRepository.findFirstByOrderById(); }
            if(project != null){
                return project.getId();
            }
        }
        return null;
    }

}
